package com.y.utils;

import cn.hutool.core.util.IdUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 上传头像工具类
 * @author: Yjh
 */
public class ImgUtils {
    public static String uploadImage(MultipartFile file){
        String originalFilename = file.getOriginalFilename(); // 源文件
        String prefix = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        String uuid = IdUtil.simpleUUID(); // 重命名
//        System.out.println("prefix = " + prefix);
//        System.out.println("suffix = " + suffix);
//        System.out.println(prefix+uuid+suffix);
        String fileName = prefix+uuid+suffix;
        String rootPath = System.getProperty("user.dir");
        String filePath = rootPath+"\\src\\main\\resources\\static\\img\\"+fileName;
        System.out.println(filePath);
        File newFile = new File(filePath);
        try {
            file.transferTo(newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "/img/"+fileName;
    }
}
